//
//  TCLiveEngineViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/16.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class TCLiveEngineViewController: NSViewController {
    
    override func loadView() {
        let view = NSView.init(frame:NSMakeRect(0, 0, kTRTCWinWidth, kTRTCWinHeight))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.black.cgColor
        self.view = view
    }
    
    var isHasUser:Bool = false {
        didSet {
            let width =  isHasUser ? kTRTCUserWidth:0
            self.userVc.view.snp.updateConstraints { (make) in
                make.width.equalTo(width)
            }
            self.userVc.view.isHidden = !isHasUser
            
            //重新设置
            var trtcSize = self.trtcVc.view.frame.size
            trtcSize.width = isHasUser ? trtcSize.width+kTRTCUserWidth:trtcSize.width
            self.view.window?.setContentSize(trtcSize)
            self.view.window?.screenSafe()
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        self.title = "会议号："+(Td.sh().params.roomId ?? "00000000")
        
        // 是否独立弹出
        NotificationCenter.default.addObserver(self,selector: #selector(hasUserVc(notification:)),name: MTHasUserVcNotification,object: nil)
    }
    
    
    lazy var userVc: TCLiveUSEREngViewController = {
        let vc =  TCLiveUSEREngViewController.init(isStand: false)
        return vc
    }()
    
    lazy var trtcVc: TCLiveViewController = {
        let vc =  TCLiveViewController.init()
        return vc
    }()
    
    func setupUI() {
        self.addChild(trtcVc)
        self.addChild(userVc)
        
        self.view.addSubview(userVc.view)
        userVc.view.snp.makeConstraints { (make) in
            make.width.equalTo(0)
            make.height.right.top.equalToSuperview()
        }
        
        self.view.addSubview(trtcVc.view)
        trtcVc.view.snp.makeConstraints { (make) in
            make.left.height.top.bottom.equalToSuperview()
            make.right.equalTo(userVc.view.snp.left)
        }
        
        
        //设置默认
        self.userVc.view.isHidden = true
        //self.userVc.isHasUser = true
    }
    
    
}

//MARK:- 通知
extension TCLiveEngineViewController {
    
    @objc func hasUserVc(notification:Notification)  {
        if let obj = notification.object as? (Bool,Int) {
            self.isHasUser = obj.0
            self.userVc.userNav.selectIndex = obj.1
            self.userVc.switchTo(index: obj.1)
        }else if let obj = notification.object as? Bool {
            self.isHasUser = obj
        }
    }
}


extension TCLiveEngineViewController:NSWindowDelegate {
    func windowWillEnterFullScreen(_ notification: Notification) {
        
    }
    func windowWillExitFullScreen(_ notification: Notification) {
        
    }
    func windowDidEnterFullScreen(_ notification: Notification) {
        self.trtcVc.enterFullScreen()
    }
    
    func windowDidExitFullScreen(_ notification: Notification) {
        self.trtcVc.exitFullScreen()
    }
    
    func windowWillClose(_ notification: Notification) {
        print(#function)
    }
}
